package com.bdqn.easybuy1.dao.impl;

import com.bdqn.easybuy1.dao.BaseDao;
import com.bdqn.easybuy1.dao.UserDao;
import com.bdqn.easybuy1.entities.User;
import com.bdqn.easybuy1.utils.EmptyUtils;
import com.bdqn.easybuy1.utils.Pager;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao<User> implements UserDao {
    /**
     * 新增用户信息
     *
     * @param conn 数据库连接对象
     * @param user 封装用户信息的对象
     * @return 返回受影响的行数
     * @throws Exception
     */
    @Override
    public int insert(Connection conn, User user) throws Exception {
        String sql = " INSERT INTO `easybuy_user`(`loginName`,`userName`,`password`,`sex`,`identityCode`,`email`,`mobile`) VALUES(?,?,?,?,?,?,?) ";
        return super.update(conn, sql, user.getLoginName(),
                user.getUserName(),
                user.getPassword(),
                user.getSex(),
                user.getIdentityCode(),
                user.getEmail(),
                user.getMobile());
    }

    /**
     * 更新用户信息
     *
     * @param conn 数据库连接对象
     * @param user 封装用户信息的对象
     * @return 返回受影响的行数
     * @throws Exception
     */
    @Override
    public int update(Connection conn, User user) throws Exception {
        String sql = " UPDATE `easybuy_user` SET `loginName` = ?,`userName` = ?,`type` = ?,`sex` = ?," +
                " `identityCode` = ?, `email` =?, `mobile` =? WHERE `id` =?  ";
        return super.update(conn, sql, user.getLoginName(),
                user.getUserName(),
                user.getType(),
                user.getSex(),
                user.getIdentityCode(),
                user.getEmail(),
                user.getMobile(),
                user.getId());
    }

    /**
     * 根据id删除用户信息
     *
     * @param conn 数据库连接对象
     * @param id   用户id
     * @return 返回受影响的行数
     * @throws Exception
     */
    @Override
    public int deleteUserById(Connection conn, String id) throws Exception {
        String sql = " DELETE FROM `easybuy_user` WHERE `id` = ? ";
        return super.update(conn, sql, id);
    }

    /**
     * 分页查询用户信息
     *
     * @param conn       数据库连接对象
     * @param startIndex 查询的索引
     * @param pageSize   每页显示的数据量
     * @return 返回用户列表
     * @throws Exception
     */
    @Override
    public List<User> getUserList(Connection conn, Integer startIndex, Integer pageSize) throws Exception {
        String sql = "  SELECT `id`,`loginName`,`password`,`userName`,`sex`,`identityCode`,`email`,`mobile`,`type` " +
                " FROM `easybuy_user`  LIMIT  ?,?";
        return super.getBeanList(conn, sql, startIndex, pageSize);
    }

    /**
     * 查询用户分页的总条数
     *
     * @param conn 数据库连接对象
     * @return 返回分页的总条数
     * @throws Exception
     */
    @Override
    public Integer count(Connection conn) throws Exception {
        String sql = " SELECT COUNT(*) FROM `easybuy_user` WHERE 1=1 ";
        Long retVal = super.getValue(conn, sql);
        return retVal.intValue();
    }

    /**
     * 根据id查询用户信息
     *
     * @param conn      数据库连接对象
     * @param id        用户id
     * @param loginName 用户登录名
     * @return 返回用户对象
     * @throws Exception
     */
    @Override
    public User getUserById(Connection conn, Integer id, String loginName) throws Exception {
        List<Object> paramsList = new ArrayList<Object>();
        StringBuffer sql = new StringBuffer(" SELECT `id`,`loginName`,`userName`,`password`,`sex`,`identityCode`,`email`,`mobile`,`type` FROM `easybuy_user` WHERE 1=1  ");

        if (EmptyUtils.isNotEmpty(id)) {
            sql.append(" AND `id` = ? ");
            paramsList.add(id);
        }

        if (EmptyUtils.isNotEmpty(loginName)) {
            sql.append(" AND `loginName` = ? ");
            paramsList.add(loginName);
        }
        return super.getBean(conn, sql.toString(), paramsList.toArray());
    }
}
